﻿using System.Linq;
using System;
using System.Data.Linq;

namespace UniMana.Data
{
    public static class System_UserData
    {
        public static bool UserValidated(string strUserName)
        {
            bool validated = false;
            DBMain data = new DBMain();
            System_User sur = data.m_DCReadOnly.System_Users.Where(p => p.UserName == strUserName).FirstOrDefault();
            if (sur == null)
            {
                validated = true;
            }
            return validated;
        }

        //public static IQueryable GetUserName()
        //{
        //    DBMain data = new DBMain();

        //    IQueryable iQuery = (from su in data.m_DCReadOnly.System_Users
        //                         select new
        //                         {
        //                             ID = su.System_UserKey.ToString(),
        //                             DescName = su.UserName.ToString(),
        //                             ParentID = su.Catalogue_DepartmentKey.ToString(),
        //                             FullName = su.FullName.ToString(),
        //                             Description = su.Description
        //                         }).Union(from cd in data.m_DCReadOnly.Catalogue_Departments
        //                                  select new
        //                                  {
        //                                      ID = cd.Catalogue_DepartmentKey.ToString(),
        //                                      DescName = cd.Name.ToString(),
        //                                      ParentID = "",
        //                                      FullName = "",
        //                                      Description =""
        //                                  });
        //    return iQuery;
        //}


        public static IQueryable GetList()
        {
            DBMain data = new DBMain();
            return data.m_DCReadOnly.System_Users;
        }
        
        public static IQueryable GetByDepartment(int _intDepartment)
        {
            DBMain data = new DBMain();
            return data.m_DCReadOnly.System_Users.Where(p=>p.Catalogue_DepartmentKey ==_intDepartment);
        }
        public static IQueryable GetAllUserByGroup(int _intGroupKey)
        {
            DBMain data = new DBMain();
            return data.m_DCReadOnly.System_User_Groups;
        }
        public static IQueryable GetUserByGroup(int _intGroupKey)
        {
            DBMain data = new DBMain();
            var iQuery = from su in data.m_DCReadOnly.System_Users
                         from sug in data.m_DCReadOnly.System_User_Groups
                         from pn in data.m_DCReadOnly.Personnel_Personnels
                         from dp in data.m_DCReadOnly.Catalogue_Departments
                         where
                         sug.System_GroupKey == _intGroupKey &&
                         su.System_UserKey == sug.System_UserKey &&
                         su.Personnel_PersonnelKey == pn.Personnel_PersonnelKey &&
                         pn.Catalogue_DepartmentKey == dp.Catalogue_DepartmentKey
                         select new
                         {
                             su.BeginDay,
                             su.Catalogue_DepartmentKey,
                             su.ChangePassword,
                             su.DateLogined,
                             su.Description,
                             su.DisableAccount,
                             su.EndDay,
                             FullName = pn.LastName + " "+ pn.Name,
                             su.LimitLoginNo,
                             su.LoginNo,
                             su.NerverExpire,
                             su.Password,
                             su.Personnel_PersonnelKey,
                             su.Status,
                             su.StyleInterface,
                             su.System_UserKey,
                             su.UserName,
                             DepartmentName = dp.Name
                         };
            return iQuery;
        }
        public static Guid GetPersionelKey(string strID)
        {
             DBMain data = new DBMain();

             var result = data.m_DCReadOnly.Personnel_Personnels.Where(p => p.ID == strID).FirstOrDefault();
             Guid personnelKey = Guid.Parse(UniMana.Utils.Configs.DefaultConfig.EMPTY_GUID);
             if (result != null)
                 personnelKey = result.Personnel_PersonnelKey;
             return personnelKey;
        }

        public static IQueryable GetUserOfPersionel(Guid PersonelKey)
        {
            DBMain data = new DBMain();
            var result = data.m_DCReadOnly.Personnel_Personnels.Where(p => p.Personnel_PersonnelKey == PersonelKey);
            //string strID = "";
            //if (result != null)
            //{
            //   strID =  result.ID;
            //}
            return result;
        }

        #region[An]
        /// <summary>
        /// Lấy personnalkey với userkey
        /// </summary>
        /// <param name="nSystem_User"></param>
        /// <param name="m_strUserKey"></param>
        /// <returns></returns>

        public static string ReturnPersonnalWithUserKey(this Table<System_User> nSystem_User, string m_strUserKey)
        {
            var values = from t in nSystem_User
                         where t.System_UserKey.ToString() == m_strUserKey
                         select t;
            if (values.Count() > 0)
                return values.FirstOrDefault().Personnel_PersonnelKey.ToString();
            else
                return Utils.Configs.DefaultConfig.EMPTY_GUID;
        }
        #endregion
    }
}
